#ifndef __LOG_H_INCLUDED
#define __LOG_H_INCLUDED

#include <scos.h>

#define LOG_LEAVEPROGRAM() raise(SIGINT)

#define LOG_FUNC __func__
#define STREAM(stream) __FILE__, __LINE__, LOG_FUNC, stream
#define abortOnError(s, v)         {if ((v))\
        {lprintf(s, "Function dependency failed: " #v); LOG_LEAVEPROGRAM();}}
#define returnOnError(s, v)        {if ((v))\
        {lprintf(s, "Function dependency failed: " #v); return(-1);}}
#define abortOnNegative(s, v)         {if ((v)<0)\
        {lprintf(s, "Function dependency failed: " #v); LOG_LEAVEPROGRAM();}}
#define returnOnNegative(s, v)        {if ((v)<0)\
        {lprintf(s, "Function dependency failed: " #v); return(-1);}}
#define abortOnBounds(s, v, l, u)  {if (((v) < (l)) || ((v) > (u)))\
        {lprintf(s, "Bounds check failed: " #l " < " #v " > " #u);\
         LOG_LEAVEPROGRAM();}}
#define returnOnBounds(s, v, l, u) {if (((v) < (l)) || ((v) > (u)))\
        {lprintf(s, "Bounds check failed: " #l " < " #v " > " #u);\
         return(-1);}}
#define abortOnNULL(s, v)          {if (!(v))\
        {lprintf(s, "NULL check failed: " #v); LOG_LEAVEPROGRAM();}}
#define returnOnNULL(s, v)         {if (!(v))\
        {lprintf(s, "NULL check failed: " #v); return(-1);}}
#define abortErrorl(x)           {\
        {lprintf x; LOG_LEAVEPROGRAM();}}
#define returnErrorl(x)          {\
        {lprintf x; return(-1);}}

typedef struct log_default_streams_s
{
  int crit;                   ///< Logging stream for critical errors.
  int err;                    ///< Logging stream for normal errors.
  int warning;                ///< Logging stream for warnings.
  int notice;                 ///< Logging stream for notifications.
  int info;                   ///< Logging stream for general information.
  int debug;                  ///< Logging stream for debug information.
  int crawl;                  ///< Logging stream for step-by-step debugging.
} log_default_streams_t;             

void lprintf(const char* file, int line, const char* function,
             int stream, const char* msg, ...);

#endif /* __LOG_H_INCLUDED */
